#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

import config
import models

class TxtHandler(webapp.RequestHandler):
    def get(self,ip):
		if ip=="self":
			ip=self.request.remote_addr

		self.response.headers['Content-Type'] = 'text/plain'
		entry=models.getIpInfo(ip)
		if entry is not None:
			country_name=entry['country_name']
			if 'city' in entry:
				city=entry['city']
				self.response.out.write('%s/%s' %(city,country_name));
			else:
				self.response.out.write('%s' %(country_name));
			self.response.set_status(200)
		else:
			self.response.out.write("Location not found\n");
			self.response.set_status(404)


def main():
    application = webapp.WSGIApplication([('/stxt/([^/]+)?', TxtHandler)],debug=config.debug)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
